package org.digitarts.rtms.spring.collectors.jmx;

import java.util.HashMap;
import java.util.Map;

import javax.management.ObjectName;

public class JMXCollection
{
	protected ObjectName			targetObjectName		= null;
	protected String[]				attributeNames			= null;
	protected String[]				metricTypes				= null;
	protected String				defaultMetricType		= null;
	protected String[]				metricNames				= null;
	protected String[]				segments				= null;
	protected Map<String, String>	attributeMetricTypeMap	= new HashMap<String, String>();
	protected Map<String, String>	attributeMetricNameMap	= new HashMap<String, String>();
	protected boolean				inited					= false;

	public JMXCollection()
	{
		super();
	}

	public synchronized void init() throws Exception
	{
		if (inited)
			return;
		// Update default metric type.
		if (defaultMetricType != null)
		{
			for (int i = 0; i < this.attributeNames.length; i++)
			{
				attributeMetricTypeMap.put(attributeNames[i], defaultMetricType);
			}
		}
		// Update special metric types.
		if (null != metricTypes)
		{
			if (this.attributeNames.length == this.metricTypes.length)
			{
				for (int i = 0; i < this.attributeNames.length; i++)
				{
					if (null != metricTypes[i] && 0 < metricTypes[i].length())
					{
						attributeMetricTypeMap.put(this.attributeNames[i], metricTypes[i]);
					}
				}
			}
			else
			{
				throw new Exception("Unequal number of attribute names and metric types");
			}
		}
		if (metricNames.length == 1 && metricNames[0].equals("*"))
		{
			for (int i = 0; i < this.attributeNames.length; i++)
			{
				attributeMetricNameMap.put(attributeNames[i], attributeNames[i]);
			}
		}
		else
		{
			if (this.attributeNames.length == this.metricNames.length)
			{
				for (int i = 0; i < this.attributeNames.length; i++)
				{
					attributeMetricNameMap.put(this.attributeNames[i], metricNames[i]);
				}
			}
			else
			{
				throw new Exception("Unequal number of attribute names and metric names");
			}
		}
		inited = true;

	}

	public ObjectName getTargetObjectName()
	{
		return targetObjectName;
	}

	public void setTargetObjectName(ObjectName targetObjectName)
	{
		this.targetObjectName = targetObjectName;
	}

	public String[] getAttributeNames()
	{
		return attributeNames;
	}

	public void setAttributeNames(String[] attributeNames)
	{
		this.attributeNames = attributeNames;
	}

	public String[] getMetricTypes()
	{
		return metricTypes;
	}

	public void setMetricTypes(String[] metricTypes)
	{
		this.metricTypes = metricTypes;
	}

	public Map<String, String> getAttributeMetricTypeMap()
	{
		return attributeMetricTypeMap;
	}

	public String[] getMetricNames()
	{
		return metricNames;
	}

	public void setMetricNames(String[] metricNames)
	{
		this.metricNames = metricNames;
	}

	public String[] getSegments()
	{
		return segments;
	}

	public void setSegments(String[] segments)
	{
		this.segments = segments;
	}

	public Map<String, String> getAttributeMetricNameMap()
	{
		return attributeMetricNameMap;
	}

	public String getDefaultMetricType()
	{
		return defaultMetricType;
	}

	public void setDefaultMetricType(String defaultMetricType)
	{
		this.defaultMetricType = defaultMetricType;
	}

}
